package ch.pizzapp.kurier;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.rest.RestService;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import android.app.Activity;
import android.app.Fragment;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.Toast;
import ch.pizzapp.kurier.client.InfoClient;
import ch.pizzapp.kurier.data.SignUpResponceData;

import com.mobsandgeeks.saripaar.Rule;
import com.mobsandgeeks.saripaar.Validator;
import com.mobsandgeeks.saripaar.Validator.ValidationListener;
import com.mobsandgeeks.saripaar.annotation.Email;
import com.mobsandgeeks.saripaar.annotation.Required;

/**
 * A placeholder fragment containing a simple view.
 */
@EFragment(R.layout.fragment_fogot_password)
public class FogotPasswordFragment extends Fragment {

	@Required(order = 7)
	@Email(order = 2)
	@ViewById(R.id.email)
	EditText email;

	private Validator validator;

	public FogotPasswordFragment() {
	}

	public static FogotPasswordFragment newInstance() {
		FogotPasswordFragment fragment = new FogotPasswordFragment_();
		// Bundle args = new Bundle();
		// args.putParcelable(CATEGORY_DATA, categoryData);
		// fragment.setArguments(args);
		return fragment;
	}

	@AfterViews
	void iniUI() {
		validator = new Validator(this);
		validator.setValidationListener(new RegistrationActivity());
	}

	@Click({ R.id.reset_password_button })
	void onClick(View view) {
		switch (view.getId()) {
		case R.id.reset_password_button:

			validator.validate();
			break;
		default:
			break;

		}
	}

	public class RegistrationActivity implements ValidationListener {

		public void onValidationSucceeded() {

			hideKeyboard(getActivity());
			showProgress(true);
			doSignUp();
		}

		public void onValidationFailed(View failedView, Rule<?> failedRule) {
			String message = failedRule.getFailureMessage();

			if (failedView instanceof EditText) {
				failedView.requestFocus();
				((EditText) failedView).setError(message);
			} else {
				showMessge(message);
			}
		}

	}

	public void hideKeyboard(Activity activity) {
		InputMethodManager inputMethodManager = (InputMethodManager) activity
				.getSystemService(Activity.INPUT_METHOD_SERVICE);
		// Find the currently focused view, so we can grab the correct window
		// token from it.
		View view = activity.getCurrentFocus();
		// If no view currently has focus, create a new one, just so we can grab
		// a window token from it
		if (view == null) {
			view = new View(activity);
		}
		inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
	}

	@RestService
	InfoClient infoClient;

	@Background
	void doSignUp() {

		final MultiValueMap<String, String> mvMap = new LinkedMultiValueMap<String, String>();
		mvMap.add("email", email.getText().toString());
		mvMap.add("type", "14");

		new ReachabilityTest(getActivity(), new ReachabilityTest.Callback() {

			@Override
			public void onReachabilityTestPassed() {

				try {
					setInfor(infoClient.fogotPassword(mvMap));
				} catch (Exception e) {

					System.err.println(e);
				}
			}

			@Override
			public void onReachabilityTestFailed() {
				showProgress(false);
				showMessge("Not connected ");
			}
		});
	}

	@UiThread
	void setInfor(final SignUpResponceData signUpResponceData) {

		showProgress(false);
		showMessge(signUpResponceData.getResponse().getMessage());
		
		if (!signUpResponceData.getResponse().getStatus().equals("0")) {
			getActivity().finish();
		}

	}

	@UiThread
	void showMessge(final String msg) {
		Toast.makeText(getActivity(), msg, Toast.LENGTH_LONG).show();
	}

	@ViewById(R.id.login_status)
	protected LinearLayout mLoginStatusView;
	@ViewById(R.id.sing_up_form)
	ScrollView singUpForm;

	@UiThread
	void showProgress(final boolean show) {
		mLoginStatusView.setVisibility(show ? View.VISIBLE : View.GONE);
		singUpForm.setVisibility(show ? View.GONE : View.VISIBLE);
	}

}